package net.begincode.service.code.impl;

import java.util.ArrayList;
import java.util.List;

import utils.CacheUtils;
import config.DbTemplate;
import net.begincode.bean.BcCode;
import net.begincode.bean.BcNavigation;
import net.begincode.bean.CourseChapter;
import net.begincode.service.code.CodeService;

public class CodeServiceImpl implements CodeService {
	private CodeServiceImpl() {
	}

	private static CodeServiceImpl instance = null;

	public static CodeServiceImpl getInstance() {
		if (instance == null) {
			synchronized (CodeServiceImpl.class) {
				if (instance == null) {
					instance = new CodeServiceImpl();
				}
			}
		}
		return instance;
	}

	@Override
	public int saveCode(BcCode bcCode) {
		
		StringBuilder addSql = new StringBuilder();
		addSql.append(" insert into  bc_code(code_info,code_abstract,code_content,code_url,discuss_flag,keyword,praise,view_count,bc_navigation_id,bc_user_id,writer,is_begincode,check_flag,cdate) ");
		addSql.append(" values(?,?,?,?,?,?,?,?,?,?,?,?,?,now()) ");
		return DbTemplate.insert(addSql.toString(), new Object[]{
			bcCode.getCode_info(),bcCode.getCode_abstract(),bcCode.getCode_content(),bcCode.getCode_url(),bcCode.getDiscuss_flag(),bcCode.getKeyword(),bcCode.getPraise(),bcCode.getView_count(),bcCode.getBc_navigation_id(),bcCode.getBc_user_id(),bcCode.getWriter(),bcCode.getIs_begincode(),bcCode.getCheck_flag()
		});
	}

	@Override
	public List<BcCode> selCodeLimitN(int n) {
		
		String selCode = " select * from bc_code  order by  cdate desc limit ? ";
		return DbTemplate.queryForList(selCode, new Object[]{n}, BcCode.class);
	}

	@Override
	public List<BcCode> selNewCodeByChapterIdCache(String chapterId, int n) {
		Object dbEle = CacheUtils.getEleByNameAndKey(CacheUtils.DOWN_CACHE, chapterId+"newCode"+n);
		if(dbEle != null){
			return (List<BcCode>)dbEle;
		}else{
			String seSql = "select bc.* from  chapter_code_rel ccr left join bc_code bc on ccr.bc_code_id = bc.bc_code_id where ccr.course_chapter_id = ? limit ? ";
			List<BcCode> ccs = DbTemplate.queryForList(seSql, new Object[]{chapterId,n}, BcCode.class);
			CacheUtils.addEleByParam(CacheUtils.DOWN_CACHE, chapterId+"newCode"+n, ccs);
			return ccs;
		}
	}

	@Override
	public BcCode selCodeById(String codeId) {
		
		String selSql = new String(" select * from bc_code where bc_code_id = ? ");
		return DbTemplate.queryForObject(selSql, new Object[]{codeId}, BcCode.class);
	}

	@Override
	public List<BcCode> selCodesByNavId(String navId) {
		
		String selSql  = " select * from bc_code where bc_navigation_id = ?  order by cdate desc ";
		return DbTemplate.queryForList(selSql, new Object[]{navId}, BcCode.class);
	}

	@Override
	public List<BcCode> selCodesByCourseId(String courseId, int n) {
		
		String selSql = " select bc.* from chapter_code_rel ccr left join course_chapter cc on ccr.course_chapter_id = cc.course_chapter_id left join bc_code bc on ccr.bc_code_id = bc.bc_code_id where cc.bc_course_id = ? order by cdate desc limit ? ";
		return DbTemplate.queryForList(selSql, new Object[]{courseId,n}, BcCode.class);
	}

	@Override
	public List<BcCode> selHotCodeLimit(int n) {
		
		String selSql = " select * from bc_code  order by view_count desc limit ? ";
		return DbTemplate.queryForList(selSql, new Object[]{n}, BcCode.class);
	}

	@Override
	public List<BcCode> selCodeByParam(BcCode bcCode) {
		
		String selSql = " select * from bc_code ";
		return DbTemplate.queryForList(selSql, null, BcCode.class);
	}

	@Override
	public int updCode(BcCode bcCode) {
		List paraList = new ArrayList();
		StringBuilder updSql = new StringBuilder();
		updSql.append(" update bc_code  set code_info = ? ");
		paraList.add(bcCode.getCode_info());
		if(bcCode.getCode_abstract() != null){
			updSql.append(" ,code_abstract = ? ");
			paraList.add(bcCode.getCode_abstract());
		}
		if(bcCode.getCode_content() != null){
			updSql.append(" ,code_content = ? ");
			paraList.add(bcCode.getCode_content());
		}
		if(bcCode.getCode_url() != null){
			updSql.append(" ,code_url = ? ");
			paraList.add(bcCode.getCode_url());
		}
		if(bcCode.getDiscuss_flag() != null){
			updSql.append(" ,discuss_flag = ? ");
			paraList.add(bcCode.getDiscuss_flag());
		}
		if(bcCode.getKeyword() != null){
			updSql.append(" ,keyword = ? ");
			paraList.add(bcCode.getKeyword());
		}
		if(bcCode.getPraise() != 0){
			updSql.append(" ,praise = ? ");
			paraList.add(bcCode.getPraise());
		}
		if(bcCode.getView_count() != 0){
			updSql.append(" ,view_count = ? ");
			paraList.add(bcCode.getView_count());
		}
		if(bcCode.getBc_navigation_id() != 0){
			updSql.append(" ,bc_navigation_id = ? ");
			paraList.add(bcCode.getBc_navigation_id());
		}
		if(bcCode.getBc_user_id() != 0){
			updSql.append(" ,bc_user_id = ? ");
			paraList.add(bcCode.getBc_user_id());
		}
		if(bcCode.getWriter() != null){
			updSql.append(" ,writer = ? ");
			paraList.add(bcCode.getWriter());
		}
		if(bcCode.getIs_begincode() != null){
			updSql.append(" ,is_begincode = ? ");
			paraList.add(bcCode.getIs_begincode());
		}
		if(bcCode.getCheck_flag() != null){
			updSql.append(" ,check_flag = ? ");
			paraList.add(bcCode.getCheck_flag());
		}
		paraList.add(bcCode.getBc_code_id());
		updSql.append(" where bc_code_id = ? ");
		return DbTemplate.update(updSql.toString(), paraList.toArray());
	}

	@Override
	public BcCode selCodeByIdCache(String codeId) {
		// TODO Auto-generated method stub
		String selSql = new String(" select * from bc_code where bc_code_id = ? ");
		Object dbEle = CacheUtils.getEleByNameAndKey(CacheUtils.CODE_CACHE, codeId);
		if(dbEle != null){
			return (BcCode)dbEle;
		}else{
			BcCode ccs = DbTemplate.queryForObject(selSql, new Object[]{codeId}, BcCode.class);
			CacheUtils.addEleByParam(CacheUtils.CODE_CACHE,codeId, ccs);
			return ccs;
		}
	}

	@Override
	public List<BcCode> selCodesByCourseIdCache(String courseId, int n) {
		// TODO Auto-generated method stub
		String selSql = " select bc.* from chapter_code_rel ccr left join course_chapter cc on ccr.course_chapter_id = cc.course_chapter_id left join bc_code bc on ccr.bc_code_id = bc.bc_code_id where cc.bc_course_id = ? order by cdate desc limit ? ";
		Object dbEle = CacheUtils.getEleByNameAndKey(CacheUtils.CODE_CACHE, "codeCourse"+courseId);
		if(dbEle != null){
			return (List<BcCode>)dbEle;
		}else{
			List<BcCode> ccs = DbTemplate.queryForList(selSql, new Object[]{courseId,n}, BcCode.class);
			CacheUtils.addEleByParam(CacheUtils.CODE_CACHE,"codeCourse"+courseId, ccs);
			return ccs;
		}
	}

	@Override
	public List<BcCode> selCodesByNavIdCache(String navId) {
		// TODO Auto-generated method stub
		String selSql  = " select * from bc_code where bc_navigation_id = ?  order by cdate desc ";
		Object dbEle = CacheUtils.getEleByNameAndKey(CacheUtils.CODE_CACHE, "codenav"+navId);
		if(dbEle != null){
			return (List<BcCode>)dbEle;
		}else{
			List<BcCode> ccs = DbTemplate.queryForList(selSql, new Object[]{navId}, BcCode.class);
			CacheUtils.addEleByParam(CacheUtils.CODE_CACHE,"codenav"+navId, ccs);
			return ccs;
		}
	}

	@Override
	public List<BcCode> selHotCodeLimitCache(int n) {
		// TODO Auto-generated method stub
		String selSql = " select * from bc_code  order by view_count desc limit ? ";
		Object dbEle = CacheUtils.getEleByNameAndKey(CacheUtils.CODE_CACHE, "hotCode"+n);
		if(dbEle != null){
			return (List<BcCode>)dbEle;
		}else{
			List<BcCode> ccs = DbTemplate.queryForList(selSql, new Object[]{n}, BcCode.class);
			CacheUtils.addEleByParam(CacheUtils.CODE_CACHE,"hotCode"+n, ccs);
			return ccs;
		}
	}

	@Override
	public void updCodeViewCount(String id, int count) {
		// TODO Auto-generated method stub
		String updSql = " update bc_code set view_count = view_count +?  where bc_code_id = ? ";
		DbTemplate.update(updSql, new Object[]{count,id});
	}
	
	
}
